package com.sky.java_nio;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

import org.junit.Test;

/**
 * 
 *@Description: http://ifeve.com/java-nio-channel-to-channel/
 *@Author:hjt 
 *@Version:1.0.0
 */
public class NIO_ChannelToChannel {
	/**
	 * 在Java NIO中，如果两个通道中有一个是FileChannel，那你可以直接将数据从一个channel
	 * （译者注：channel中文常译作通道）传输到另外一个channel
	 */
	
	@Test
	public void testTransfer() throws Exception {
		//FileChannel的transferFrom()方法可以将数据从源通道传输到FileChannel中
		//（译者注：这个方法在JDK文档中的解释为将字节从给定的可读取字节通道传输到此通道的文件中
		RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");
		FileChannel      fromChannel = fromFile.getChannel();

		RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
		FileChannel      toChannel = toFile.getChannel();

		long position = 0;
		long count = fromChannel.size();

		toChannel.transferFrom(fromChannel, position, count);
		
		//transferTo()方法将数据从FileChannel传输到其他的channel中
		fromChannel.transferTo(position, count, toChannel);

	}

}
